package com.xpd.spring.aop;

import java.lang.annotation.Target;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

//动态代理
//基于接口实现
public class JDKproxy {
    public static void main(String[] args) {
        Person fangdong=new fangdong();
        Person person=new zhongjie(fangdong);
        person.say();
    }
    public static <T>T proxy(T target){
        Object o = Proxy.newProxyInstance(target.getClass().getClassLoader(),
                target.getClass().getInterfaces(), new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args)
                            throws Throwable {
                        try {
                            System.out.println("前置增强");
                            Object invoke = method.invoke(target, args);
                            System.out.println("返回增强");
                        } catch (Exception e) {
                            System.out.println("异常增强");
                            throw new RuntimeException(e);
                        } finally {
                            System.out.println("后置增强");
                        }
                        return null;
                    }
                });
        return (T) o;
    }
}
